<?php
/**
*优惠券模块
*2013/7/10
*/
class CouponsAction extends Action
{
	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(6,$perm_arr)){
			$this->error("无权操作");
		}
	}

	public function lists(){
		$Coupons=M('Coupons');
		$coupons=$Coupons->group("cou_id")->order("cou_id desc")->select();
		for($i=0;$i<count($coupons);$i++){
			$coupons[$i]['num']=$Coupons->where("cou_id=".$coupons[$i]['cou_id'])->count();
		}
		$this->coupons=$coupons;
		$this->display();
	}

	public function cou_detail(){
		$map['cou_id']=$this->_get('id');
		$Coupons=M('Coupons');

		import("ORG.Util.Page");
		$count=$Coupons->where($map)->count();//->where($map)
		$Page=new Page($count,20);

		$coupons=$Coupons->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();

		$Page->setConfig('header', '条');
		$Page->setConfig('prev', '<img src="__APP__/statics/up.gif">');
		$Page->setConfig('next', '<img src="__APP__/statics/down.gif">');
		$Page->setConfig('theme', "%upPage%  %linkPage% %downPage%  共%totalPage%页");
        $page = $Page->show();
		for($i=0;$i<count($coupons);$i++){
			$coupons[$i]['passcode']=implode('-',str_split($coupons[$i]['passcode'],4));
		}
		$this->coupons=$coupons;
		$this->num=$count;
		$this->assign("page", $page);
		$this->display();
	}
	
	public function add(){
		$Coupons=M('Coupons');
		$ck=$Coupons->group("cou_id")->order("cou_id desc")->limit(1)->select();
		$this->code=$ck[0]['cou_id']+1;
		$this->now=date("Y-m-d",time());
		$this->display();
	}

	public function product(){
		set_time_limit(600);
		$Coupons=M('Coupons');
		$data['cou_id']=$this->_post('coupons');
		$map['cou_id']=$this->_post('coupons');
		if($Coupons->where($map)->select()){
			$this->error("此优惠券卡号已被占用，请更换！");
		}
		$user=session('opadmin');
		if(!empty($user)){
			$data['op_admin']=$user;
		}

		$data['type']=$this->_post('type');
		$data['cou_amount']=$this->_post('amount');
		$data['min_money']=$this->_post('min_money');
		$data['begin_time']=strtotime($this->_post('begin_time'));
		$data['end_time']=strtotime($this->_post('end_time')." 23:59:59");
		$data['add_time']=time();
		$num=$this->_post('nums');
		foreach($data as $v){
			if(empty($v)){
				$this->error("有未输入的值！请检查");
			}
		}
		$y=array('A','B','2','3','4','5','6','7','8','9','C','D','E','F','G','2','3','4','5','6','7','8','9','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','2','3','4','5','6','7','8','9');
		$k=0;
		for($i=0;$i<$num;$i++){
			$tt=date("ym");
			for($j=0;$j<12;$j++){
			   $tt.=$y[rand(0,46)];
			}
			$data['passcode']=$tt;
			$re=$Coupons->add($data);
			if($re){$k++;}
		}
		echo "成功生成优惠券：".$k."张<br>";
		echo "<a href=''>下载</a>";
		//implode('',str_split($tt,4))
	}

	public function download(){
		$Coupons=M('Coupons');
		$map['cou_id']=$this->_get('id');
		$map['status']='active';
		$map['owner']=0;
		$coupons=$Coupons->where($map)->select();
		header("Content-Type: application/vnd.ms-execl");   
		header("Content-Disposition: attachment; filename=yh-".$map['cou_id'].".xls");   
		header("Pragma: no-cache");   
		header("Expires: 0");
		echo "卡号"."\t";
		echo "卡密"."\t";
		echo "面额"."\t";
		echo "开始时间"."\t";
		echo "失效时间"."\t";
		echo "\t\n";
		for($i=0;$i<count($coupons);$i++){
			echo $coupons[$i]['cou_id']."\t";
			echo implode('-',str_split($coupons[$i]['passcode'],4))."\t";
			echo $coupons[$i]['cou_amount']."\t";
			echo date("Y-m-d",$coupons[$i]['begin_time'])."\t";
			echo date("Y-m-d",$coupons[$i]['end_time'])."\t";
		    echo "\t\n";  
		}
	}

	public function del(){
		$type=$this->_get('type');
		$Coupons=M('Coupons');
		switch($type){
			case 'all':
				$map['cou_id']=$this->_post('id');
			    $Coupons->where($map)->delete();
				echo '1';
				break;
			case 'one':
				$map['passcode']=str_replace('-','',$this->_post('code'));
			    $Coupons->where($map)->delete();
				echo '1';
				break;
		}
	}


}


?>

